﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Modelling.Components;
using System.Drawing.Printing;

namespace ITAP.ShprossesBuilder.Editor
{
    public partial class ImageExportForm : Form
    {
        private List<ImageExportInfo> imageExportInfo;

        public List<ImageExportInfo> ImageExportInfo
        {
            get { return imageExportInfo; }
            set { imageExportInfo = value; }
        }

        private int selectedIndex = 0;
        public ImageExportForm(List<ModelCell> modelsToExport, PrinterSettings printerSettings)
        {

            imageExportInfo = new List<ImageExportInfo>();
            InitializeComponent();
            foreach (ModelCell modelCell in modelsToExport)
            {
                DimensionsPreferences preferences = (GlobalPreferences.Preferences.Clone() as Preferences).DraftDimensions;
                ImageExportInfo info;
                if (printerSettings != null)
                    info = new ImageExportInfo(modelCell, preferences, (PageSettings)printerSettings.DefaultPageSettings.Clone());
                else
                    info = new ImageExportInfo(modelCell, preferences, null);
                
                imageExportInfo.Add(info);
                listBoxModels.Items.Add(info);
                listBoxModels.SelectedIndex = selectedIndex;            
            }

            this.modelPreviewControl.PreviewChanged += new ModelPreviewChanged(modelPreviewControl_PreviewChanged);
        }

        void modelPreviewControl_PreviewChanged()
        {
            this.RefreshModelsListBox();
        }

        private bool isRefreshing = false;
        private void RefreshModelsListBox()
        {
            isRefreshing = true;
            listBoxModels.BeginUpdate();
            List<ImageExportInfo> info = new List<ImageExportInfo>();
            for (int i = 0; i < listBoxModels.Items.Count; i++)
                info.Add(listBoxModels.Items[i] as ImageExportInfo);
            listBoxModels.Items.Clear();
            foreach (ImageExportInfo _info in info)
                listBoxModels.Items.Add(_info);
            listBoxModels.SelectedIndex = selectedIndex;
            listBoxModels.EndUpdate();
            isRefreshing = false;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void listBoxModels_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!isRefreshing)
            {
                if (listBoxModels.SelectedItem != null)
                {
                    this.selectedIndex = listBoxModels.SelectedIndex;
                    this.modelPreviewControl.ShowInfo(listBoxModels.SelectedItem as ImageExportInfo);
                    this.RefreshModelsListBox();
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {

        }

    }
}
